package de.fhd.pms;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Phrase;

import de.fhd.pms.dto.RechnungDTO;
import de.fhd.pms.model.Fahrt;

/**
 * 
 * Class generating an Invoice for finished {@link Fahrt} objects. It uses the
 * itextpdf-5.1.3.jar library. Its content:
 * 
 * <ul>
 * <li>{@link Paragraph} objects which creates header, main and footer parts</li>
 * <li>a {@link Document} object which puts the {@link Paragraph} objects
 * together and creates a new Invoice Page
 * </ul>
 * 
 * @return Invoice
 * @throws Exception
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 * 
 */
public class Rechnung {

	public Rechnung(RechnungDTO rechnung) {


		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());

		DecimalFormat de = new DecimalFormat("#0.00", new DecimalFormatSymbols(
				Locale.GERMANY));

		double satz = 19.00;
		double netto = rechnung.getKosten() / (100 + satz) * 100;
		double mwst = rechnung.getKosten() - netto;

		// Der Pfad muss dem aktuellen Workspace angepasst werden

		String FILE = "C:/EclipseWorkspace/AutovermietungNeu/WebContent/pdf/"
				+ rechnung.getId() + ".pdf";

		int tag = cal.get(Calendar.DAY_OF_MONTH);
		int monat = cal.get(Calendar.MONTH) + 1;
		int jahr = cal.get(Calendar.YEAR);

		String datum = "München, " + tag + "." + monat + "." + jahr;
		try {
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(FILE));
			document.open();
			
			Font gross = new Font();
			gross.setStyle("bold");
			gross.setSize(22);
			Font klein = new Font();
			klein.setSize(8);

			Paragraph header = new Paragraph();
			Paragraph main = new Paragraph();
			Paragraph footer = new Paragraph();
			Paragraph date = new Paragraph();
			
			date.setAlignment(Element.ALIGN_RIGHT);
			
			
			
			newLine(date,3);
			date.add(new Paragraph(datum));
			newLine(date,1);
			
			header.add(new Paragraph("Autovermietung München GmbH"));
			header.add(new Paragraph("Musterstraße 5"));
			header.add(new Paragraph("80995 München"));
			newLine(header,2);
			header.add(new Paragraph(rechnung.getName()));
			header.add(new Paragraph(rechnung.getStrasse()));
			header.add(new Paragraph(rechnung.getOrt()));
			header.add(new Paragraph());


			newLine(main,2);
			main.add(new Paragraph("Rechnung", gross));
		
			
			newLine(main,2);
			

			newLine(footer,16);
			footer.add(new Paragraph(
					"___________________________________________________________________________"));
			footer.add(new Paragraph(
					"Sparkasse München                                  www.autovermietung-muenchen.de                                   Steuernummer: 123/1234567",
					klein));
			footer.add(new Paragraph(
					"Kto: 123456879                                         info@autovermietung-muenchen.de                                  UstID: 132465798",
					klein));
			footer.add(new Paragraph(
					"BLZ: 100100100                                        089/123456", klein));
			

			PdfPTable table = new PdfPTable(2);
			table.setHorizontalAlignment(Element.ALIGN_LEFT);

			PdfPCell cell = new PdfPCell(new Phrase(" "));
			cell.setColspan(2);


			table.addCell("Fahrzeug:");
			table.addCell(rechnung.getMarke() + " " + rechnung.getModell());
			table.addCell("Stunden: ");
			table.addCell(rechnung.getDauer());
			table.addCell("Kilometer: ");
			table.addCell(rechnung.getKm());
			table.addCell(cell);
			table.addCell("Nettobetrag: ");
			table.addCell(de.format(netto) + "€");
			table.addCell("MwSt: ");
			table.addCell(de.format(mwst) + "€");
			table.addCell("Bruttobetrag: ");
			table.addCell(de.format(rechnung.getKosten()) + "€");

			main.add(table);

			document.add(header);
			document.add(date);
			document.add(main);
			document.add(footer);

			document.newPage();
			document.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void newLine(Paragraph paragraph, int lines) {

		for (int i = 0; i < lines; i++) {
			paragraph.add(new Paragraph());
		}

	}
	


}